var POPKEY = "popkey";
var s;
var Popularize = {
    init:function(){
        s = new wxee();
        s.signature(null);
        this.getData();
        //if(!getStorageStringOrInteger(POPKEY)){
        //    $("body").append(popdom);
        //}
    },
    eventLisenter:function(){
        $(".intenthtml").click(function(){
            window.location.href=$(this).attr("href");
        });
    },
    getData:function(){
        new AjaxRequest({
            type: "get",
            url:apiService.suitShareInit,
            param: "",
            callBack:function(res){
                if(res.code==302){
                    window.location.href= res.url;
                    return;
                }else if(res.success){
                    $(".persons").text(res.payload.awardedPersons);
                    var data = res.payload.items;
                    var rules = res.payload.productAwardRules;
                    for(var i in rules) {
                        var dom = `<p><span>${i}</span><a>${rules[i]}</a><span>元</span></p>`;
                        $(".tag").append(dom);
                    }
                    for (let i = 0; i < data.length; i++) {
                        buildDom(data[i],".content");
                    }
                }else{
                    alert(res.message);
                }
            },
            failBack: function(){
            }
        });
    },
	shareUrl:function(type,data){
        new AjaxRequest({
            type: "get",
            // url:apiService.saveProductShare+"?recordId="+data.introKey+"&goodsId="+data.goodsId,
            url:'http://zysc.netpats.com/ref/saveProductShare?recordId='+data.introKey+'&goodsId='+data.goodsId,
            param: "",
            callBack:function(res){
                alert(res.message);
            },
            failBack: function(res){
            }
        });
    },
}
function removes(dom){
	$(dom).remove();
}
function shareClick(dom,data){
    var role = $(dom).attr("role");
    if(role==2){
        if(!data.goodsShareLink){
            alert("信息不完整");
            return;
        }
        alert(data.goodsShareLink.link);
        var shareData = {
            title: data.goodsShareLink.title,
            desc: data.goodsShareLink.desc,
            link: data.goodsShareLink.link,
            imgUrl: data.goodsShareLink.imgUrl,
            callback:function(){
                $(".popwindow").remove();
                Popularize.shareUrl(role,data.goodsShareLink);
            }
        }
        s.setData(shareData);
        showPop(data.canAwardAmt);
    }else{
        window.location.href=data.goodsDetailLink;
    }

}
function oncilk(dom){
    window.location.href=$(dom).attr("href");
}
function showPop(money){
    var popdom = `<div class="popwindow" onclick="removes(this)">
        <!--<p>目前已有目前已有目前已有目前已有目前已有目前已有目前已有目前已有目前已有目前已有目前已有</p>-->
        <div class="pop-ur">
            <img src="../../img/popullarize/p2.png"/>
        </div>
        <div class="popbot flexs-center">
            <div class="pop-l">
                <img src="../../img/popullarize/s.png"/>
                <p>发送好友</p>
            </div>
            <div class="pop-l">
                <img src="../../img/popullarize/p1.png"/>
                <p>分享朋友圈</p>
            </div>
        </div>
        <div class="toasttext">
            戳这里，分享给好友或朋友圈，对方支付成功后，您可获得
            <span style="color:#fff008;font-size:0.9rem">${money}</span>元奖励！
        </div>
    </div>`;
    $("body").append(popdom);
}
function buildDom(data,id){
	var d = JSON.stringify(data).replace(/\"/g,"'");
    var dom = `<div class="pitem ">
                <div class="flexs-center p">
                    <div class="header title flexs-center">【下单赢现金】${data.goodName}￥${data.goodPrice}</div>
                </div>
                <div class="item">
                    <div class="flexs-center introduc">
                        <img src="${data.goodImage}"/>
                        <p>${data.goodDesc}</p>
                    </div>
                    <div class="flexs-pace-between i-bot">
                        <div class="l flexs-center">
                            <p><span>可获得: </span><a>${data.canAwardAmt}</a><span>元奖励</span></p>
                        </div>
                        <div class="r flexs-center"><span role="1" class="" onclick="shareClick(this,${d})">查看详情</span><a role="2" class="header" onclick="shareClick(this,${d})">直接分享</a></div>
                    </div>
                </div>
            </div>`;
    $(id).append(dom);
	//document.getElementById(id.no).onclick=function (dom,val){ return function(){shareClick(dom,val);}}(1,data);
}